import time
import unittest

from selenium.webdriver.support.select import Select

from crm.base.crm_browser import BrowserOperation
from crm.base.crm_browserex import BrowserOperationEx
from crm.base.crm_use_browser import UseBrowser
from crm.util.exl_operation import ExlOpera
from crm.util.yaml_operation import YamlOperation


class CustomerAssignPage:

    def __init__(self):
        self.bo = BrowserOperation(UseBrowser.driver)
        self.boex = BrowserOperationEx(UseBrowser.driver)
        self.yo=YamlOperation()
        self.exl = ExlOpera()
        self.exl.get_sheet('客户分配用例参数')

    #点击左边的客户分配后，切回右边框架
    def customer_assign(self):
        self.boex.change_frame(self.bo.find_element('xpath',self.yo.get_locator('Brow', 'left_frame')))
        self.bo.click('link_text', '客户分配')
        self.boex.change_frame(self.yo.get_locator('Brow', 'right_frame'))

    # 分配1个客户
    def customer_assign_one(self,name):
        strip=self.bo.search_strip('xpath',self.yo.get_locator('CustomerAssignPage','strip_xpath'))
        for i in range(strip):
            # 每行的姓名
            element= self.bo.get_text('xpath',self.yo.get_locator('CustomerAssignPage','line_xpath') + str(i+2) + ']/td[2]/div')
            if element==name:
                #每行的勾选框
                self.bo.click('xpath',self.yo.get_locator('CustomerAssignPage','line_xpath') + str(i+2) + ']/td[1]/div/input')
                break
        #点击分配
        self.bo.click('xpath',self.yo.get_locator('CustomerAssignPage', 'assign_submit_xapth'))
        #选择分配的员工
        select_el=self.bo.find_element('name',self.yo.get_locator('CustomerAssignPage', 'select_name'))
        select = Select(select_el)
        select.select_by_visible_text(self.exl.get_cell(1,3))
        self.bo.click('name',self.yo.get_locator('CustomerAssignPage', 'submit'))

    #返回分配成功信息
    def get_customer_assign_one(self):
        return self.bo.alert_opertaion()

    # 客户分配，全选
    def customer_assign_all(self):
        self.bo.click('xpath', self.yo.get_locator('CustomerAssignPage', 'all_assign_xpath'))
        time.sleep(2)

    # 搜索客户分配列表
    def search_customerassgin(self, cus_name):
        self.bo.alert_opertaion(1)


    # 判断每一行的第1个，是否全部选中
    def get_customer_assign_all(self, star_tr=2, end_tr=6):
        for i in range(star_tr, end_tr):
            element= self.bo.find_element('xpath',self.yo.get_locator('CustomerAssignPage','line_xpath') + str(i) + ']/td[1]/div/input')
            print(element)
            if not element.is_selected():
                return False
        else:
            return True





